/* *
 * Copyright (c) 2004-2005 Mocha Almond Fudge AI, SIT KMUTT. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *  o Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer. 
 *     
 *  o Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution. 
 *     
 *  o Neither the name of Mocha Almond Fudge AI, SIT KMUTT nor the names of 
 *    its contributors may be used to endorse or promote products derived 
 *    from this software without specific prior written permission. 
 */
package jboard.model.ai;

import java.util.Vector;

import jboard.model.BoardPosition;
import jboard.model.HeuristicMovement;
import jboard.model.IBoardConstant;
import jboard.model.Move;
import jboard.model.ai.search.MinimaxNode;
import jboard.model.ai.search.MinimaxTree;

/**
 * 2 pile look up (defensive + agressive) AI
 * 
 * @author Vashira Ravipanich
 */
public class PureMinimaxAI implements AI, IBoardConstant{
	private MinimaxTree gameTree;
	private Vector bestMoveList;
	private Move aiMove;
	private BoardPosition aiBPS;
	
	/**
	 * Constructor
	 */
	public PureMinimaxAI() {
		gameTree = new MinimaxTree(TWO_PILE);
	    gameTree.setDepth(1);
	}
	
	private HeuristicMovement makeMinimaxMove(MinimaxNode minimaxRootNode) {
	    gameTree.setRoot(minimaxRootNode);
	    gameTree.constructTree();
	    bestMoveList = gameTree.getBestPath();
	    
	    System.out.println("Debug : bestMoveList.size() = " + bestMoveList.size());
	    return (HeuristicMovement)bestMoveList.get(0);
	}
	
	public BoardPosition makeMove(BoardPosition currentBPS) {
	    //	1. Set BoardPosition so AI can lookup in his own board
        setMyBoardPS(currentBPS);
        
        HeuristicMovement minimaxMove = makeMinimaxMove((prepareRootNode(this.aiBPS)));	// Only minimax!!
        this.aiMove = minimaxMove.getMove();
        
		return aiBPS.makeMove(this.aiMove, BLACK);
	}
	
	private MinimaxNode prepareRootNode(BoardPosition boardPosition) {
	    return new MinimaxNode(boardPosition, 0, 0); // (0, 0) first mean no nedd to calc Heuristic Val, second mean level 0 <MAX>
	}
	
	public Move getMoved() {
        return this.aiMove;
    }

    private void setMyBoardPS(BoardPosition aiBoardPS) {
        this.aiBPS = aiBoardPS;
    }
}
